/**
 * Created by leo on 2/2.
 */
angular.module('panels')
    .directive('panelItilToDoTask', function ($http, tabService, LoginInfo, loadConf, $timeout, $log, Promise) {
        return {
            link: function (scope) {
                Promise.all({
                    setting: loadConf('panel-itil-todo-task.yml'),
                    loginInfo: LoginInfo
                }).then(function (result) {
                    var setting = result.setting, loginInfo = result.loginInfo, timeoutId;
                    scope.more = setting.more;
                    if (setting.processList) {
                        scope.todoList = setting.processList;
                    }
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        $http.jsonp(setting.countUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID)).success(function (data) {
                            if (scope.todoList) { // 如果配置了processList， 就只显示配置的工单类型
                                var todoMap = {};
                                angular.forEach(scope.todoList, function (todo) {
                                    todoMap[todo.id] = todo;
                                });
                                angular.forEach(data, function (item) {
                                    var todo = todoMap[item.PROCESSID];
                                    if (todo) {
                                        todo.count = item.CNT;
                                    }
                                });
                            } else { // 否则全部展示返回结果中的工单类型
                                scope.todoList = [];
                                angular.forEach(data, function (item) {
                                    if(item.CNT){
                                        this.push({id: item.PROCESSID, name: item.NAME, count: item.CNT});
                                    }
                                }, scope.todoList);
                            }
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }

                        $log.debug('panel-update data: itil-to-do-task');
                    }());
                    scope.openTodoListPage = function (todo) {
                        tabService.addTab({
                            name: todo.name,
                            url: scope.getUrl(todo)
                        });
                    };
                    scope.getUrl = function (todo) {
                        return setting.viewUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID || '').replace('{processId}', parseInt(todo.id) || '')
                    }
                });
            },
            templateUrl: 'app/panel/itil/to-do-task.html'
        };
    });